<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Member_model extends CI_Model {

    function member_all_count($q = '') {
        if (!empty($q)) {
            $this->db->like('member_username', $q)
                    ->or_like('member_firstname', $q)
                    ->or_like('member_lastname', $q)
                    ->or_like('member_email', $q);
        }
        return $this->db->count_all_results('members');
    }

    function member_all_show($limit, $start, $q) {
        if (!empty($q)) {
            $this->db->like('member_username', $q)
                    ->or_like('member_firstname', $q)
                    ->or_like('member_lastname', $q)
                    ->or_like('member_email', $q);
        }
        $this->db->limit($limit, $start);
        $query = $this->db->select('member_id,member_fb_id,member_username,member_email,member_firstname,member_lastname,is_active,member_address,member_gender,member_birthday')->get('members');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
        return false;
    }

    function member_get($id) {
        return $this->db->select('member_id,member_username,member_email,member_firstname,member_lastname,is_active,member_address,member_gender,member_birthday,member_contact,member_thumb,member_role')->where('member_id', $id)
                        ->get('members')->row_array();
    }

    function delete($id) {
        //Check & Delete thumb if exist 
        $this->db->select('member_thumb')
                ->from('members')
                ->where('member_id', $id)
                ->limit(1);

        $exist_thumb = $this->db->get()->row();

        if (isset($exist_thumb->member_thumb) && $exist_thumb->member_thumb != '') {


            foreach (glob('uploads/thumbs/*/' . $exist_thumb->member_thumb) as $file) {
                unlink($file);
            }
            unlink(('uploads/' . $exist_thumb->member_thumb));
        }

        $this->db->delete('members', array('member_id' => $id));
        return TRUE;
    }

    function member_create($create_data) {
        $this->load->library('bcrypt');
        $create_data['member_password'] = $this->bcrypt->hash_password($create_data['member_password']);
        $create_data['created_at'] = time();
        $this->db->insert('members', $create_data);

        return $this->db->insert_id();
    }

    function member_edit($id, $edit_data) {


        /* Has changed a password */
        if ($edit_data['member_password']) {
            $this->load->library('bcrypt');
            $edit_data['member_password'] = $this->bcrypt->hash_password($edit_data['member_password']);
        } else {
            unset($edit_data['member_password']);
        }

        return $this->db->where('member_id', $id)->update('members', $edit_data);
    }

    function member_status($id, $status) {
        if ($status == '1') {
            $st = '0';
        } else {
            $st = '1';
        }
        return $this->db->where('member_id', $id)
                        ->update('members', array('is_active' => $st));
    }

    function check_email($email) {

        return $this->db->where('member_email', $email)
                        ->count_all_results('members') == 0;
    }

    function check_username($username) {

        return $this->db->where('member_username', $username)
                        ->count_all_results('members') == 0;
    }

    function add_thumb($id, $data) {



        //Check & Delete thumb if exist 
        $this->db->select('member_thumb')
                ->from('members')
                ->where('member_id', $id)
                ->limit(1);

        $exist_thumb = $this->db->get()->row();

        if (isset($exist_thumb->member_thumb) && $exist_thumb->member_thumb != '') {


            foreach (glob('uploads/thumbs/*/' . $exist_thumb->member_thumb) as $file) {
                unlink($file);
            }
            unlink(('uploads/' . $exist_thumb->member_thumb));
        }


        $thumb = $data['media_name'];

        $this->db->where('member_id', $id)
                ->update('members', array('member_thumb' => $thumb));

        return TRUE;
    }

    function count_member() {
        $sql = 'SELECT member_role,COUNT(member_id) as count FROM members GROUP BY member_role';
        return $this->db->query($sql);
    }

    function member_search($keyword, $page, $limit, $role = '') {

        $this->db->select('member_id,member_firstname,member_lastname,member_thumb,member_role');

        $where = "  (member_firstname LIKE '%$keyword%' 
                    OR member_lastname LIKE '%$keyword%' 
                    OR member_id LIKE '%$keyword%'
                    OR member_username LIKE '%$keyword%'
                    OR member_email LIKE '%$keyword%')";

        $this->db->where($where);

        $this->db->limit(($page - 1) * $limit + $limit, ((($page - 1) * $limit)));

        if ($role != '') {
            $this->db->where('member_role', $role);
        }
        return $this->db->get('members')->result();
    }

    function member_search_count($keyword, $role = '') {
        $this->db->select('member_id');
        $where = "  (member_firstname LIKE '%$keyword%' 
                    OR member_lastname LIKE '%$keyword%' 
                    OR member_id LIKE '%$keyword%'
                    OR member_username LIKE '%$keyword%'
                    OR member_email LIKE '%$keyword%')";

        $this->db->where($where);
        if ($role != '') {
            $this->db->where('member_role', $role);
        }

        return $this->db->get('members')->num_rows();
    }

}
